<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template #object="{row}">
      <div class="flex">
        <img style="height: 36px;" :src="row.value.head_img" />
        <p>{{row.value.title}}</p>
        <p>（数量：{{row.value.num}}）</p>
      </div>
    </template>
    <template #hr="{row}">
      ---------------------------{{row.value}}---------------------------
    </template>
  </forms>
</template>

<script>
export default {
  props: ['__iframe'],
  data(){
    let app = this, id = this.$get('id'), btns = {
      '不同意'(){
        app.config.action_status.value = 2;
        setTimeout(function (){app.$refs['forms'].submit()},20)
      },
      '同意_primary'(){
        app.config.action_status.value = 1;
        setTimeout(function (){app.$refs['forms'].submit()},20)
      },
    };
    let model = {name:this.$get('model_name'), alias:this.$get('model_alias','')};
    let forms_config = {
      order: {label:'订单项目', name:'order', type:'object', value:[], tips:''},
      title: {label:'退款类型', name:'title', tips:''},
      reason_user: {label:'退款理由', name:'reason_user', tips:''},
      order_pay_money: {label:'订单金额', name:'order_pay_money', tips:'订单金额，包含运费，如果已经发货，运费是不会退款的'},
      all_refund_money: {label:'退款金额', name:'all_refund_money', tips:''},
      need_deliver: {label:'退款模式', name:'need_deliver', type:'select_one', config:['仅退款','退货退款'], is_info:true, tips:''},
      action_status: {label:'审核结果', name:'action_status', type:'hidden', config:'同意|1,不同意|2', value:'2', tips:''},
      pay_type: {label:'退款方式', name:'pay_type', type:'radio', config:[{content:'原路退回',value:''},{content:'线下退款',value:'offline'}], tips:''},
      reason_admin: {label:'处理意见', name:'reason_admin', type:'text_more', tips:''},

      hr1: {label:'', name:'hr1', type:'hr', value:'以下为退货收货信息', tips:''},
      deliver_user: {label:'收货人', name:'deliver_user', type:'text_one', tips:''},
      deliver_phone: {label:'收货电话', name:'deliver_phone', type:'text_one', tips:''},
      deliver_address: {label:'收货地址', name:'deliver_address', type:'text_one', tips:''},
      deliver_no: {label:'物流单号', name:'deliver_no', tips:''},
    }
    if (id){
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      model: model,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    onsubmit(formData){
      let url = `/admin/order_refund/edit_action?id=`+this.id;
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          if (this.model.name=='tenant_package'){
            iframe.exe('/tenant_package_order/index', '$refs.list_url.list');
          }else {
            iframe.exe('/order/index?model_name='+this.model.name, '$refs.list_url.list');
          }
          iframe.hide('edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  watch: {
    'config.need_deliver.value'(v){
      this.config.hr1.remove =
          this.config.deliver_user.remove =
              this.config.deliver_phone.remove =
                  this.config.deliver_address.remove =
                      this.config.deliver_no.remove = !v;
    },
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;

    ajax({url:'/admin/order_refund/info?id='+this.id, cache:true}, (res)=>{
      this.model.name = res.data.model_name;
      res.data.order_pay_money = res.data.order.pay_money;
      res.data.all_refund_money = res.data.refund_money;
      try {
        let extend_array = JSON.parse(res.data.extend);
        if ('refund_money' in extend_array) res.data.all_refund_money = extend_array.refund_money
      } catch (e) {}
      res.data.pay_type = '';//退款方式-默认原路退回
      this.$refs['forms'].init(res.data);
      setTimeout(()=>{
        if (!res.data.need_deliver && res.data.order.skip_deliver!=1 && res.data.order.status>2){
          this.$refs['forms'].errors['need_deliver'] = '此订单已经发货，但用户不需要退货，请谨慎处理';
        }
      },10)
      return false;
    }, ()=>{
      if (this.__iframe){
        iframe.remove('edit');
      }else {
        this.$router.push('index');
      }
    })

    //其他初始化操作
  }
}
</script>
